package com.qfedu.controller;

import com.qfedu.bean.User;
import com.qfedu.dao.InsertDao;
import com.qfedu.dao.impl.InseretDaoImpl;
import com.qfedu.service.InsertService;
import com.qfedu.service.impl.InsertServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet(name = "InsertServlet",urlPatterns = "/insert")
public class InsertServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User inputUser=new User();
        InsertDao insertDao=new InseretDaoImpl();
        try {
            BeanUtils.populate(inputUser,req.getParameterMap());
            InsertService insertService=new InsertServiceImpl();
            User insert = insertService.insert(inputUser);
            insert.setUsername(insert.getUsername());
            insert.setPassword(insert.getPassword());
            insertDao.insert(insert);
            System.out.println(inputUser);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
